/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GameGL;

import AdvanceMath.Vector2;
import GameGL.GameGL.Input;
import Render.Renderer;
import com.sun.opengl.util.Animator;
import com.sun.opengl.util.FPSAnimator;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.media.opengl.GLCanvas;

/**
 *
 * @author RomulusAaron
 */
public class GameBase implements WindowListener{
    GameGL game;
    Frame gameFrame;
    FPSAnimator animator;
    GLCanvas canvas;
    Graphics g;
    
    public GameBase(GameGL game, String title){
        this.game=game;
        gameFrame=new Frame(title);
        canvas=new GLCanvas();
        canvas.addGLEventListener(game);
        animator=new FPSAnimator(canvas, 60, true);
        gameFrame.add(canvas);
        gameFrame.addWindowListener(this);
        gameFrame.addKeyListener(game.input);
        gameFrame.addMouseListener(game.input);
        gameFrame.addMouseMotionListener(game.input);
        gameFrame.addMouseWheelListener(game.input);
        game.setGameFrame(gameFrame);
        game.setGameBase(this);
        game.setGraphics(canvas.getGraphics());
    }

    
    public void Start(){
        game.start();
        gameFrame.setSize((int)game.ScreenSize.getX(), (int)game.ScreenSize.getY());
        gameFrame.setVisible(true);
        animator.start();
        gameFrame.setFocusable(true);
        gameFrame.requestFocus();
    }
    
    public void Quit(){
        WindowEvent j=new WindowEvent(gameFrame, WindowEvent.WINDOW_CLOSING);
        gameFrame.dispatchEvent(j);
    }

    public void windowOpened(WindowEvent e) {
    }

    public void windowClosing(WindowEvent e) {
        new Thread(new Runnable() {

            public void run() {
                try {
                    animator.stop();
                    System.exit(0);
                } catch (Exception e) {
                    if(e.getMessage().equals("Already stopped")){
                        System.exit(0);
                    }else{
                        animator.stop();
                        System.exit(0);
                    }
                }
            }
        }).start();
    }

    public void windowClosed(WindowEvent e) {
    }

    public void windowIconified(WindowEvent e) {
    }

    public void windowDeiconified(WindowEvent e) {
    }

    public void windowActivated(WindowEvent e) {
    }

    public void windowDeactivated(WindowEvent e) {
    }
    
}
